/*
 * ResourceLoader.h
 *
 * The resource loader for the whole project. Keeps track of and loads all resources.
 * WARNING: the "static" keyword means something different in java, c++ does all sorts of crazy shit with static. Be careful!
 *
 *  Created on: Aug 21, 2013
 *      Author: zapmunk
 */

#pragma once

#include "Resource.h"
#include "PNGResource.h"
#include "TextResource.h"

#include "RESOURCES.h"
#define init(name, path, type) \
	static type ## Resource name

class ResourceLoader {
public:
	class LoadException {
	public:
		const std::string resName, resPath;
		LoadException(std::string resName, std::string resPath): resName(resName), resPath(resPath) {}
		std::string getName() const {return resName;}
		std::string getPath() const {return resPath;}
		std::string getMsg() const {return (std::string)"" + "Failed to load " + resName + " from " + resPath + "!";}
	};
private:
	static std::vector<Resource*> preload, autoload, manual;
	//Loads all resources from the given vector
	static void loadAll(std::vector<Resource*>* resources, ProgressTracker* tracker = NULL) throw(LoadException);
public:
	//Call at the beginning to make sure it can keep track of all it's resources
	static void initResources();

	//Load all preload resources
	static void loadPreload() throw(LoadException) {loadAll(&preload);}
	//Load all autoload resources
	static void loadAutoload(ProgressTracker* tracker = NULL) throw(LoadException){loadAll(&autoload, tracker);}

	//Unloads all resources. Call at the end for cleanup
	static void unloadAll();

	RESOURCES_PRELOAD;
	RESOURCES_AUTOLOAD;
	RESOURCES_MANUAL;
};

#undef RESOURCES_PRELOAD
#undef RESOURCES_AUTOLOAD
#undef RESOURCES_MANUAL
#undef init
